using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Resources;
using System.Windows.Forms;
using FlexCell;
using Infragistics.Win.UltraWinTabControl;
using Infragistics.Win.UltraWinTabs;
using PluginFrame;
using static FlexCell.Grid;

namespace PcrDataAnalysis
{
    public class FormSelectStandardCurve : Form
    {
        private IContainer components;

        protected Panel panelButtons;

        protected Label label2;

        protected Button btnClose;

        protected Button btnConfirm;

        private GroupBox grpStandardCurve;

        private Label label1;

        private Label label3;

        private Label label4;

        protected ResourceManager mResourceManager;

        protected IApplication mApplication;

        protected List<IPcrExperimentProject> mAnalysisProjects;

        protected Dictionary<string, List<IPcrExperimentProject>> mStandardCurveProjects;

        protected Dictionary<string, List<IPcrStandardCurve>> mStandardCurves;

        protected Dictionary<string, List<string>> mStandardCurveFileNames;

        protected Dictionary<string, IPcrStandardCurve> mCurrentImportStandardCurve;

        protected Dictionary<string, Grid> mProjectGridDictionary = new Dictionary<string, Grid>();

        protected IPcrExperiment mPcrExperiment;

        protected IConfigurationService mConfigurationService;

        protected IPcrDataService mPcrDataService;

        protected IPcrLogService mPcrLogService;

        protected int mStdCurveTabCount;

        protected bool mIsShowAll;

        protected List<IPcrStandardCurve> mStandardCurveInFiles;

        public List<IPcrStandardCurve> StandardCurveInFiles
        {
            get
            {
                return mStandardCurveInFiles;
            }
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && components != null)
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void InitializeComponent()
        {
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(PcrDataAnalysis.FormSelectStandardCurve));
            this.panelButtons = new System.Windows.Forms.Panel();
            this.label2 = new System.Windows.Forms.Label();
            this.btnClose = new System.Windows.Forms.Button();
            this.btnConfirm = new System.Windows.Forms.Button();
            this.grpStandardCurve = new System.Windows.Forms.GroupBox();
            this.label4 = new System.Windows.Forms.Label();
            this.label1 = new System.Windows.Forms.Label();
            this.label3 = new System.Windows.Forms.Label();
            this.panelButtons.SuspendLayout();
            base.SuspendLayout();
            this.panelButtons.AccessibleDescription = null;
            this.panelButtons.AccessibleName = null;
            resources.ApplyResources(this.panelButtons, "panelButtons");
            this.panelButtons.BackgroundImage = null;
            this.panelButtons.Controls.Add(this.label2);
            this.panelButtons.Controls.Add(this.btnClose);
            this.panelButtons.Controls.Add(this.btnConfirm);
            this.panelButtons.Font = null;
            this.panelButtons.ForeColor = System.Drawing.SystemColors.ControlText;
            this.panelButtons.Name = "panelButtons";
            this.label2.AccessibleDescription = null;
            this.label2.AccessibleName = null;
            resources.ApplyResources(this.label2, "label2");
            this.label2.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
            this.label2.Font = null;
            this.label2.Name = "label2";
            this.btnClose.AccessibleDescription = null;
            this.btnClose.AccessibleName = null;
            resources.ApplyResources(this.btnClose, "btnClose");
            this.btnClose.BackgroundImage = null;
            this.btnClose.Font = null;
            this.btnClose.Name = "btnClose";
            this.btnClose.UseVisualStyleBackColor = true;
            this.btnClose.Click += new System.EventHandler(btnClose_Click);
            this.btnConfirm.AccessibleDescription = null;
            this.btnConfirm.AccessibleName = null;
            resources.ApplyResources(this.btnConfirm, "btnConfirm");
            this.btnConfirm.BackgroundImage = null;
            this.btnConfirm.Font = null;
            this.btnConfirm.Name = "btnConfirm";
            this.btnConfirm.UseVisualStyleBackColor = true;
            this.btnConfirm.Click += new System.EventHandler(btnConfirm_Click);
            this.grpStandardCurve.AccessibleDescription = null;
            this.grpStandardCurve.AccessibleName = null;
            resources.ApplyResources(this.grpStandardCurve, "grpStandardCurve");
            this.grpStandardCurve.BackgroundImage = null;
            this.grpStandardCurve.Font = null;
            this.grpStandardCurve.Name = "grpStandardCurve";
            this.grpStandardCurve.TabStop = false;
            this.label4.AccessibleDescription = null;
            this.label4.AccessibleName = null;
            resources.ApplyResources(this.label4, "label4");
            this.label4.Name = "label4";
            this.label1.AccessibleDescription = null;
            this.label1.AccessibleName = null;
            resources.ApplyResources(this.label1, "label1");
            this.label1.Name = "label1";
            this.label3.AccessibleDescription = null;
            this.label3.AccessibleName = null;
            resources.ApplyResources(this.label3, "label3");
            this.label3.Name = "label3";
            base.AccessibleDescription = null;
            base.AccessibleName = null;
            resources.ApplyResources(this, "$this");
            base.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.BackgroundImage = null;
            base.Controls.Add(this.label4);
            base.Controls.Add(this.label3);
            base.Controls.Add(this.label1);
            base.Controls.Add(this.grpStandardCurve);
            base.Controls.Add(this.panelButtons);
            this.Font = null;
            base.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
            base.Icon = null;
            base.MaximizeBox = false;
            base.MinimizeBox = false;
            base.Name = "FormSelectStandardCurve";
            base.ShowIcon = false;
            base.ShowInTaskbar = false;
            this.panelButtons.ResumeLayout(false);
            base.ResumeLayout(false);
            base.PerformLayout();
        }

        public FormSelectStandardCurve(IApplication app, IPcrExperiment pcrExperiment, List<IPcrExperimentProject> pcrExperimentProjects, Dictionary<string, IPcrStandardCurve> currentImportStandardCurve, bool isShowAll)
        {
            InitializeComponent();
            mAnalysisProjects = pcrExperimentProjects;
            mApplication = app;
            mConfigurationService = (IConfigurationService)mApplication.GetService("ConfigurationService");
            mPcrDataService = (IPcrDataService)mApplication.GetService("PcrDataService");
            mPcrLogService = (IPcrLogService)mApplication.GetService("PcrLogService");
            mPcrExperiment = pcrExperiment;
            mCurrentImportStandardCurve = currentImportStandardCurve;
            mIsShowAll = isShowAll;
            mResourceManager = ResourceManager.CreateFileBasedResourceManager("PcrDataAnalysis", app.GetResourceFolder() + GetType().Namespace + "\\", null);
            mStandardCurveProjects = new Dictionary<string, List<IPcrExperimentProject>>();
            mStandardCurves = new Dictionary<string, List<IPcrStandardCurve>>();
            mStandardCurveFileNames = new Dictionary<string, List<string>>();
            btnConfirm.Enabled = false;
            GetStandardCurveInFiles();
            if (mStandardCurveInFiles != null || mStandardCurveInFiles.Count > 0)
            {
                UltraTabControl ultraTabControl = GenerateStandardCurveTabControl();
                grpStandardCurve.Controls.Add(ultraTabControl);
                mStdCurveTabCount = ultraTabControl.Tabs.Count;
            }
            else
            {
                mStdCurveTabCount = 0;
            }
        }

        private void GetStandardCurveInFiles()
        {
            mStandardCurveInFiles = new List<IPcrStandardCurve>();
            string path = mApplication.GetAssemblyPath() + mConfigurationService.StandardCurveDataFolder;
            if (!Directory.Exists(path))
            {
                return;
            }
            string[] files = Directory.GetFiles(path, "*.std");
            string[] array = files;
            foreach (string text in array)
            {
                IPcrStandardCurve pcrStandardCurve = mPcrDataService.LoadStandardCurve(text);
                if (pcrStandardCurve != null && TestStandardCurveCellID(pcrStandardCurve))
                {
                    string fileName = Path.GetFileName(text);
                    if (pcrStandardCurve.FileName != fileName)
                    {
                        pcrStandardCurve.FileName = fileName;
                    }
                    mStandardCurveInFiles.Add(pcrStandardCurve);
                }
                Application.DoEvents();
            }
        }

        private bool TestStandardCurveCellID(IPcrStandardCurve pcrStandardCurve)
        {
            for (int i = 0; i < pcrStandardCurve.CellIDs.GetLength(0); i++)
            {
                for (int j = 0; j < pcrStandardCurve.CellIDs.GetLength(1); j++)
                {
                    if (pcrStandardCurve.CellIDs[i, j] != null && pcrStandardCurve.CellIDs[i, j].Count > 0)
                    {
                        return true;
                    }
                }
            }
            return false;
        }

        public int GetStandardCurveTabCount()
        {
            return mStdCurveTabCount;
        }

        public Dictionary<string, IPcrStandardCurve> GetPcrStandardCurves()
        {
            Dictionary<string, IPcrStandardCurve> dictionary = new Dictionary<string, IPcrStandardCurve>();
            foreach (IPcrExperimentProject mAnalysisProject in mAnalysisProjects)
            {
                Grid val = mProjectGridDictionary[mAnalysisProject.Name];
                if (val == null)
                {
                    continue;
                }
                for (int i = 1; i < val.Rows; i++)
                {
                    if (val.Cell(i, 1).BooleanValue && mStandardCurves.ContainsKey(mAnalysisProject.Name))
                    {
                        dictionary.Add(mAnalysisProject.Name, mStandardCurves[mAnalysisProject.Name][i - 1]);
                        break;
                    }
                }
            }
            return dictionary;
        }

        private void InitializeForm()
        {
        }

        private UltraTabControl GenerateStandardCurveTabControl()
        {
            //IL_010e: Unknown result type (might be due to invalid IL or missing references)
            //IL_0115: Expected O, but got Unknown
            //IL_02a7: Unknown result type (might be due to invalid IL or missing references)
            //IL_02b1: Expected O, but got Unknown
            UltraTabControl ultraTabControl = new UltraTabControl();
            ((ISupportInitialize)ultraTabControl).BeginInit();
            ultraTabControl.Dock = DockStyle.Fill;
            ultraTabControl.Size = new Size(500, 200);
            ultraTabControl.Name = "Standard Curve Tab Control";
            ultraTabControl.ViewStyle = ViewStyle.VisualStudio2005;
            ultraTabControl.TabLayoutStyle = TabLayoutStyle.SingleRowAutoSize;
            ultraTabControl.TabOrientation = TabOrientation.Default;
            ultraTabControl.ScrollButtons = TabScrollButtons.Always;
            ultraTabControl.AllowTabClosing = false;
            ultraTabControl.AllowTabMoving = false;
            ultraTabControl.Style = ((mAnalysisProjects.Count <= 1) ? UltraTabControlStyle.Wizard : UltraTabControlStyle.Default);
            ((ISupportInitialize)ultraTabControl).EndInit();
            foreach (IPcrExperimentProject mAnalysisProject in mAnalysisProjects)
            {
                if (!mIsShowAll && mCurrentImportStandardCurve != null)
                {
                    mCurrentImportStandardCurve.ContainsKey(mAnalysisProject.Name);
                }
                UltraTab ultraTab = new UltraTab();
                UltraTabPageControl ultraTabPageControl2 = (ultraTab.TabPage = new UltraTabPageControl());
                ultraTab.Text = mAnalysisProject.Name;
                ultraTab.Key = mAnalysisProject.Name;
                ultraTabPageControl2.Name = mAnalysisProject.Name;
                ultraTabPageControl2.Tag = mAnalysisProject;
                ultraTabControl.Controls.Add(ultraTabPageControl2);
                ultraTabControl.Tabs.Add(ultraTab);
                Grid val = new Grid();
                mProjectGridDictionary.Add(mAnalysisProject.Name, val);
                ((Control)(object)val).Dock = DockStyle.Fill;
                ((Control)(object)val).BringToFront();
                val.ShowResizeTip = false;
                val.DisplayRowNumber = true;
                val.DisplayRowArrow = false;
                val.DisplayFocusRect = false;
                val.AllowUserResizing = (ResizeEnum)0;
                val.ExtendLastCol = false;
                val.ScrollBars = (ScrollBarsEnum)3;
                ((Control)(object)val).Name = mAnalysisProject.Name;
                val.Cols = 5;
                val.Cell(0, 1).Text = string.Empty;
                val.Column(1).CellType = (CellTypeEnum)2;
                val.Column(1).Locked = false;
                val.Cell(0, 2).Text = mResourceManager.GetString("ProjectString");
                val.Column(2).CellType = (CellTypeEnum)0;
                val.Column(2).Locked = true;
                val.Cell(0, 3).Text = mResourceManager.GetString("FileNameString");
                val.Column(3).CellType = (CellTypeEnum)0;
                val.Column(3).Locked = true;
                val.Cell(0, 4).Text = mResourceManager.GetString("CyclesString");
                val.Column(4).CellType = (CellTypeEnum)0;
                val.Column(4).Locked = true;
                RefreshStandardCurveGridContent(mAnalysisProject, val);
                for (int i = 0; i < val.Cols; i++)
                {
                    val.Column(i).Alignment = (AlignmentEnum)10;
                    val.Column(i).AutoFit();
                }
                val.CellChange += new CellChangeEventHandler(standardCurveGrid_CellChange);
                ultraTabPageControl2.Controls.Add((Control)(object)val);
            }
            return ultraTabControl;
        }

        private void RefreshStandardCurveGridContent(IPcrExperimentProject experimentProject, Grid standardCurveGrid)
        {
            List<IPcrExperimentProject> list = new List<IPcrExperimentProject>();
            List<IPcrStandardCurve> list2 = new List<IPcrStandardCurve>();
            List<string> list3 = new List<string>();
            mStandardCurveProjects.Add(experimentProject.Name, list);
            mStandardCurves.Add(experimentProject.Name, list2);
            mStandardCurveFileNames.Add(experimentProject.Name, list3);
            IPcrStandardCurve pcrStandardCurve = null;
            IProjectItem[] projectItems = experimentProject.ProjectItems;
            foreach (IProjectItem projectItem in projectItems)
            {
                if (projectItem == null || !projectItem.ChannelConfiguration.IsSelected)
                {
                    continue;
                }
                try
                {
                    IAbsoluteAnalysisParameterConfiguration absoluteAnalysisParameterConfiguration = projectItem.TubeAnalysisParameterConfiguration[0] as IAbsoluteAnalysisParameterConfiguration;
                    if (absoluteAnalysisParameterConfiguration != null && absoluteAnalysisParameterConfiguration.ImportedStandardCurve != null)
                    {
                        pcrStandardCurve = absoluteAnalysisParameterConfiguration.ImportedStandardCurve;
                        list.Add(experimentProject);
                        list2.Add(absoluteAnalysisParameterConfiguration.ImportedStandardCurve);
                        list3.Add(absoluteAnalysisParameterConfiguration.ImportedStandardCurve.FileName);
                        break;
                    }
                }
                catch (Exception)
                {
                    pcrStandardCurve = null;
                    break;
                }
            }
            List<IPcrStandardCurve> list4 = new List<IPcrStandardCurve>();
            foreach (IPcrStandardCurve mStandardCurveInFile in mStandardCurveInFiles)
            {
                if (mStandardCurveInFile == null)
                {
                    continue;
                }
                IPcrExperimentProject pcrExperimentProject = null;
                IAnalysisResult[,] getStandardCurveData = mStandardCurveInFile.GetStandardCurveData;
                bool flag = false;
                for (int j = 0; j < getStandardCurveData.GetLength(0); j++)
                {
                    for (int k = 0; k < getStandardCurveData.GetLength(1); k++)
                    {
                        List<IPcrCell> standardPcrCells = mStandardCurveInFile.GetStandardPcrCells(j, k);
                        if (standardPcrCells != null && standardPcrCells.Count != 0)
                        {
                            IQuantitativeAnalysisResult quantitativeAnalysisResult = getStandardCurveData[j, k] as IQuantitativeAnalysisResult;
                            if (quantitativeAnalysisResult != null && !quantitativeAnalysisResult.Slope.Equals(double.NaN) && !quantitativeAnalysisResult.Intercept.Equals(double.NaN) && !quantitativeAnalysisResult.SquareError.Equals(double.NaN) && getStandardCurveData[j, k].Project != null)
                            {
                                pcrExperimentProject = getStandardCurveData[j, k].Project;
                                flag = true;
                                break;
                            }
                        }
                    }
                    if (flag)
                    {
                        break;
                    }
                }
                bool flag2 = true;
                if (pcrExperimentProject != null && experimentProject.Name.Equals(pcrExperimentProject.Name) && experimentProject.ProjectItems.Length == pcrExperimentProject.ProjectItems.Length)
                {
                    for (int l = 0; l < pcrExperimentProject.ProjectItems.Length; l++)
                    {
                        IProjectItem projectItem2 = pcrExperimentProject.ProjectItems[l];
                        IProjectItem projectItem3 = experimentProject.ProjectItems[l];
                        if (projectItem2 != null || projectItem3 != null)
                        {
                            if (projectItem2 == null || projectItem3 == null)
                            {
                                flag2 = false;
                                break;
                            }
                            if (!projectItem2.Name.Equals(projectItem3.Name))
                            {
                                flag2 = false;
                                break;
                            }
                            if (projectItem2.ChannelConfiguration.IsSelected != projectItem3.ChannelConfiguration.IsSelected)
                            {
                                flag2 = false;
                                break;
                            }
                        }
                    }
                }
                else
                {
                    flag2 = false;
                }
                if (pcrExperimentProject == null)
                {
                    list4.Add(mStandardCurveInFile);
                }
                if (flag2)
                {
                    list.Add(pcrExperimentProject);
                    list2.Add(mStandardCurveInFile);
                    list3.Add(mStandardCurveInFile.FileName);
                }
            }
            foreach (IPcrStandardCurve item in list4)
            {
                if (mStandardCurveProjects.ContainsKey(experimentProject.Name))
                {
                    mStandardCurveProjects.Remove(experimentProject.Name);
                }
                if (mStandardCurves.ContainsKey(experimentProject.Name))
                {
                    mStandardCurves.Remove(experimentProject.Name);
                }
                if (mStandardCurveInFiles.Contains(item))
                {
                    mStandardCurveInFiles.Remove(item);
                }
                if (mCurrentImportStandardCurve != null && mCurrentImportStandardCurve.ContainsKey(experimentProject.Name))
                {
                    mCurrentImportStandardCurve.Remove(experimentProject.Name);
                }
            }
            standardCurveGrid.Rows = list.Count + 1;
            string text = ((mCurrentImportStandardCurve != null && mCurrentImportStandardCurve.ContainsKey(experimentProject.Name)) ? mCurrentImportStandardCurve[experimentProject.Name].FileName : string.Empty);
            bool flag3 = false;
            for (int m = 1; m < standardCurveGrid.Rows; m++)
            {
                string fileName = Path.GetFileName(list3[m - 1]);
                standardCurveGrid.Cell(m, 2).Text = list[m - 1].Name;
                standardCurveGrid.Cell(m, 3).Text = fileName;
                if (list[m - 1].Cells == null || list[m - 1].Cells.Count == 0)
                {
                    standardCurveGrid.Row(m).Visible = false;
                }
                else
                {
                    standardCurveGrid.Cell(m, 4).Text = list[m - 1].Cells[0].PcrExperiment.GetActualAmplificationDetectionCycle().ToString();
                    standardCurveGrid.Row(m).Visible = true;
                    if (pcrStandardCurve != null && standardCurveGrid.Cell(m, 3).Text.Equals(pcrStandardCurve.FileName) && m > 1)
                    {
                        standardCurveGrid.Row(m).Visible = false;
                    }
                }
                if (text.Equals(fileName) && standardCurveGrid.Row(m).Visible)
                {
                    flag3 = true;
                    standardCurveGrid.Cell(m, 1).Text = "1";
                }
                else
                {
                    standardCurveGrid.Cell(m, 1).Text = "0";
                }
            }
            if (!flag3 && standardCurveGrid.Rows > 1)
            {
                standardCurveGrid.Cell(1, 1).Text = "1";
            }
            if (standardCurveGrid.Rows <= 1)
            {
                label3.Visible = false;
                label4.Visible = true;
                return;
            }
            label4.Visible = false;
            label3.Visible = true;
            Button button = btnConfirm;
            bool enabled = button.Enabled;
            button.Enabled = true;
        }

        private void standardCurveGrid_CellChange(object sender, CellChangeEventArgs e)
        {
            //IL_0022: Unknown result type (might be due to invalid IL or missing references)
            //IL_002c: Expected O, but got Unknown
            //IL_009c: Unknown result type (might be due to invalid IL or missing references)
            //IL_00a6: Expected O, but got Unknown
            try
            {
                if (e.Col != 1)
                {
                    return;
                }
                Grid val = (Grid)((sender is Grid) ? sender : null);
                val.AutoRedraw = false;
                val.CellChange -= new CellChangeEventHandler(standardCurveGrid_CellChange);
                if (!val.Cell(e.Row, e.Col).BooleanValue)
                {
                    val.Cell(e.Row, e.Col).Text = "True";
                }
                else
                {
                    for (int i = 1; i < val.Rows; i++)
                    {
                        if (i != e.Row)
                        {
                            val.Cell(i, e.Col).Text = "False";
                        }
                    }
                }
                val.CellChange += new CellChangeEventHandler(standardCurveGrid_CellChange);
                val.AutoRedraw = true;
                val.Refresh();
            }
            catch (Exception ex)
            {
                mPcrLogService.ShowExceptionDialogBox(ex);
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            base.DialogResult = DialogResult.Cancel;
            Close();
        }

        private void btnConfirm_Click(object sender, EventArgs e)
        {
            base.DialogResult = DialogResult.OK;
            Close();
        }
    }
}
